package com.bxshey.happyTown.config;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @作者：不想说话而已
 * @Date：2023/12/5 9:18
 *
 * @description: 自定义配置序列化器，就可以配置Object类型，并且类则不需要在去实现 Serializable接口
 */

@Configuration
public class RedisSerializerConfig {
    @Bean
    public RedisTemplate<String, Object>redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,Object>template=new RedisTemplate<>();
        template.setConnectionFactory(factory);
        //设置String类型的key的序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //设置String类型的value的序列化器
        template.setValueSerializer(new GenericFastJsonRedisSerializer());
        //设置hash类型的key的序列化器
        template.setHashKeySerializer(new StringRedisSerializer());
        //设置hash类型的value的序列化器
        template.setHashValueSerializer(new GenericFastJsonRedisSerializer());
        //初始化完成序列化的方法
        template.afterPropertiesSet();
        return template;
    }
}



